<script type="text/javascript">
var <?= $this->jsObject ?> = {
	dialogOpt: {
		width:"226px"
	},

	categoryId: <?= $this->category ? $this->category->getId() : 'null' ?>,

	categoryGridId: "<?= $this->categoryGrid->tableId ?>",

	objectGridId: "<?= $this->objectGrid->tableId ?>",

	linkedObjectGridId: "<?= $this->linkedObjectGrid->tableId ?>",		

	categoryOpt: <?= $this->categoryGrid->renderOptions() ?>,

	objectOpt: <?= $this->objectGrid->renderOptions() ?>
};

(function($, $f) {
	
$.extend($f, {
	
    listObject: function(objectId, event) {
        var gridId = $f.categoryGridId;
        var containerId = "#container_" + gridId;
        gridId = "#" + gridId;
        var rowId = $(gridId).jqGrid("getGridParam", "selrow");
        if (rowId > 0) {
            //remove category grid
            $(gridId).jqGrid("GridDestroy", gridId);
            $(containerId).append("<?= $this->objectGrid->renderHtml(true) ?>");
            this.categoryId = parseInt(rowId);
            
            //render object grid
            var opt = $f.objectOpt;
            var opt = $.extend(opt, {
                multiselect: false,
                url: $.exReqPath(opt.url) + "?" + $.param({nodeid: rowId, diff: objectId})
            });
            $(gridId).jqGrid(opt);

            //render object pager
            <?= $this->objectGrid->renderNavGrid() ?>;
        } else {
            //alert('Выберите объект');
			info_dialog("Внимание", "Пожалуйста, выберите категорию");
        }
    },

    listCategory: function(objectId, event) {
        var gridId = $f.objectGridId;
        var containerId = "#container_" + gridId;
        gridId = "#" + gridId;
        this.categoryId = null;

        $(gridId).jqGrid("GridDestroy", gridId);
        $(containerId).append("<?= $this->categoryGrid->renderHtml(true) ?>");

        //render object grid
        var opt = $f.categoryOpt;
        $(gridId).jqGrid(opt);

        //render object pager
        <?= $this->categoryGrid->renderNavGrid() ?>;
    },

    addObject: function(objectId, url, rowId, rowIndex, event) {
        var gridId = "#" + $f.objectGridId;
        var lnGridId = "#" + $f.linkedObjectGridId;
        var data = $(gridId).jqGrid("getRowData", rowId);
		if (data.id > 0) {
            $.ajax({
				url: url,
                cache: false,
                data: {id: objectId, nodeid: rowId},
                success: function(respData) {
                    $(lnGridId).jqGrid("addRowData", rowId, data, "last");
                    $(gridId).jqGrid("delRowData", rowId);
                },
                error: function() {
					//alert("Ошибка при добавлении объекта");
					info_dialog("Внимание", "Произошла ошибка при добавлении объекта", null, $f.dialogOpt);
				}
            });
        }
    },
	
    deleteObject: function(objectId, url, rowId, rowIndex, event) {
        var res = confirm("Удалить связь с объектом?");
        if (!res) { return; }
        var gridId = "#" + $f.objectGridId;
        var lnGridId = "#" + $f.linkedObjectGridId;
        var data = $(lnGridId).jqGrid("getRowData", rowId);
		if (data.id > 0) {
            $.ajax({
                url: url,
                cache: false,
                data: {id: objectId, nodeid: rowId},
                success: function(respData) {
                    if ($f.categoryId == data.category_id) {
                        $(gridId).jqGrid("addRowData", rowId, data, "last");
                    }
                    $(lnGridId).jqGrid("delRowData", rowId);
                },
                error: function() {
					//alert("Ошибка при удалении объекта");
					info_dialog("Внимание", "Произошла ошибка при удалении объекта", null, $f.dialogOpt);
				}
            });
        }
    }
});
})(jQuery, <?= $this->jsObject ?>);

</script>
<div id="container_<?= $this->categoryGrid->tableId ?>" class="content_left mr1">
    <?= $this->category ? $this->objectGrid : $this->categoryGrid ?>
</div>

<div class="content_left">
    <?= $this->linkedObjectGrid ?>
</div>

<br style="clear:both" />

<? if ($this->backurl) : ?>
<p>
<?= $this->jQueryButton(
	'save_button',
	null,
	array('events' => array(
        'click' => 'location.href = "'. $this->backurl .'"'
    )),
	array('title' => $this->backurlLabel, 'type' => 'button')
) ?>
</p>
<? endif ?>